package cn.gjculture.shopping.api.service.impl;

import cn.gjculture.shopping.api.dao.*;
import cn.gjculture.shopping.api.entity.*;
import cn.gjculture.shopping.api.service.ActivityService;
import cn.gjculture.shopping.api.service.FileService;
import cn.gjculture.shopping.api.service.GoodsService;
import cn.gjculture.shopping.api.util.DateUtil;
import cn.gjculture.shopping.api.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsDao goodsDao;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    CommonDeleteMapper commonDeleteDao;

    @Autowired
    CommonUpdateMapper commonUpdateDao;

    @Autowired
    FileService fileService;

    @Autowired
    ActivityService activityService;

    @Autowired
    PostageTemplateDao postageTemplateDao;


    @Transactional
    @Override
    public int insert(Goods goods) {
        goods.setGoodsId(UUIDUtil.getUUID());
        goods.setInsertTime(DateUtil.getTimestamp());
        goods.setUpdateTime(DateUtil.getTimestamp());
        goods.setCommodityType(Commodity.CommodityType.GOODS.getValue());
        goods.setRecommended(1);
        goods.setStatus(1);
        this.insertColors(goods);
        this.insertStand(goods);
        this.insertThumbnail(goods.getThumbnails(), goods.getGoodsId());
        return goodsDao.insert(goods);
    }


    @Override
    public List<Goods> query(Goods goods) {
        List<Goods> goodss = goodsMapper.query(goods);
        for (Goods g : goodss) {
            if (g == null){
                continue;
            }else{
                collect(g);
            }

        }
        return goodss;
    }

    private void collect(Goods goods) {
        int count=0;
        if(goods.getSales()!=null){
            count=count+goods.getSales();
        }
        if(goods.getCount()!=null){
            count=count+goods.getCount();
        }
        goods.setSales(count);
        List<String> goodsStands = goodsDao.queryStands(goods.getGoodsId());
        goods.setGoodsStands(goodsStands);
        List<String> goodsColors = goodsDao.queryColors(goods.getGoodsId());
        goods.setGoodsColors(goodsColors);
        List<Commodity.Thumbnail> thumbnails = goodsDao.queryThumb(goods.getGoodsId());
        goods.setThumbnails(thumbnails);
        activityService.queryActivityByGoods(goods);
    }

    @Override
    public int deleteMore(List<String> goodsIds) {
        if (goodsIds != null) {
            goodsMapper.deleteGoods(goodsIds);
            goodsMapper.deleteColors(goodsIds);
            goodsMapper.deleteStands(goodsIds);
            for (String goodsId : goodsIds) {
                this.deleteThemb(goodsId);
            }
            return goodsIds.size();
        }
        return 0;
    }

    @Override
    public Goods queryById(String goodsId) {
        Goods goods = goodsDao.queryById(goodsId);
        if (goods == null) {
            return goods;
        }
        List<String> goodsStands = goodsDao.queryStands(goods.getGoodsId());
        goods.setGoodsStands(goodsStands);
        List<String> goodsColors = goodsDao.queryColors(goods.getGoodsId());
        goods.setGoodsColors(goodsColors);
        List<Commodity.Thumbnail> thumbnails = goodsDao.queryThumb(goods.getGoodsId());
        goods.setThumbnails(thumbnails);
        return goods;
    }

    @Transactional
    @Override
    public int update(Goods goods) {
        goods.setUpdateTime(DateUtil.getTimestamp());
        goodsDao.deleteColor(goods.getGoodsId());
        goodsDao.deleteStand(goods.getGoodsId());
        //处理缩略图
        handleThumb(goods.getGoodsId(), goods.getThumbnails());
        this.insertColors(goods);
        this.insertStand(goods);
        return goodsMapper.updateGoods(goods);
    }

    @Override
    public void handleThumb(String goodsId, List<Commodity.Thumbnail> thumbnails) {
        if(thumbnails==null|| thumbnails.size()<=0){
            this.deleteThemb(goodsId);
            return ;
        }
        List<Commodity.Thumbnail> thumbs = goodsDao.queryThumb(goodsId);
        if(thumbs==null|| thumbs.size()<=0){
            this.insertThumbnail(thumbnails,goodsId);
            return ;
        }
        List<Commodity.Thumbnail> insertThumbs = new ArrayList<>();
        for (Commodity.Thumbnail thum : thumbnails) {
            if (!thumbs.contains(thum)) {
                insertThumbs.add(thum);
            }
        }
        List<String> delFileIds = new ArrayList<>();
        for (Commodity.Thumbnail thum : thumbs) {
            if (!thumbnails.contains(thum)) {
                delFileIds.add(thum.getFileId());
            }
        }
        if(insertThumbs!=null&& insertThumbs.size()>0){
            this.insertThumbnail(insertThumbs,goodsId);
        }
        if(delFileIds.size()>0){
            this.deleteThembByFileIds(delFileIds);
        }
    }

    private void deleteThembByFileIds(List<String> delFileIds) {
        fileService.delFiles(delFileIds.toArray(new String[]{}));
        goodsMapper.deleteThembnail(delFileIds);
    }

    @Override
    public List<Goods> queryByTypeId(String goodsTypeId) {
        List<Goods> goodsList = goodsDao.queryByTypeId(goodsTypeId);
        for (Goods goods : goodsList) {
            if (goods == null) continue;
            collect(goods);
        }
        return goodsList;
    }

    @Override
    public int recommended(Goods goods) {
        return goodsDao.recommended(goods);
    }

    @Override
    public List<Goods> queryRecommended() {
        return goodsDao.queryRecommended();
    }


    private int deleteThemb(String goodsId) {
        List<Commodity.Thumbnail> thumbnails = goodsDao.queryThumb(goodsId);
        String[] fileIds = new String[thumbnails.size()];
        int i = 0;
        for (Commodity.Thumbnail tb : thumbnails) {
            fileIds[i] = tb.getFileId();
            i++;
        }
        fileService.delFiles(fileIds);
        goodsDao.deleteThembnail(goodsId);

        return thumbnails.size();
    }

    private void insertStand(Goods goods) {
        List<String> stands = goods.getGoodsStands();
        if (stands != null && stands.size() > 0) {
            List<GoodsStand> goodsStands = new ArrayList<>();
            for (String stan : stands) {
                GoodsStand s = new GoodsStand(goods.getGoodsId(), stan, UUIDUtil.getUUID());
                goodsStands.add(s);
            }
            goodsMapper.insertStand(goodsStands);
        }
    }

    @Override
    public int insertThumbnail(List<Commodity.Thumbnail> thumbnails, String goodsId) {
        if (thumbnails != null && thumbnails.size() > 0) {
            for (Commodity.Thumbnail tb : thumbnails) {
                tb.setGoodsId(goodsId);
            }
            goodsMapper.insertThumbnail(thumbnails);
            return thumbnails.size();
        }
        return 0;
    }

    @Override
    public int updateOther(Goods goods) {
        goods.setUpdateTime(DateUtil.getTimestamp());
        return goodsMapper.updateGoods(goods);
    }

    @Override
    public List<Goods> queryDiscountCoupon() {
        List<Goods> goodsList=goodsDao.queryByActivityType(Goods.ActivityType.DISCOUNT_COUPON.getValue());
        if(!CollectionUtils.isEmpty(goodsList)){
            for (Goods goods:goodsList){
                collect(goods);
            }
        }
        return goodsList;
    }

    @Override
    public Supplier querySupplierByGoodsId(String goodsId) {
        return goodsDao.querySupplierByGoodsId(goodsId);
    }

    @Override
    public int addPostageTemplate(String postageTemplateIds, String goodsId) {

        if(!StringUtils.isEmpty(postageTemplateIds)){
            String[] ids=postageTemplateIds.split(",");

            List<GoodsMapPostageTemplate> list=new ArrayList<>();
            for(String id:ids){
                if(StringUtils.isEmpty(id)){
                    continue;
                }
                GoodsMapPostageTemplate goodsMapPostageTemplate=new GoodsMapPostageTemplate();
                goodsMapPostageTemplate.setGoodsId(goodsId);
                goodsMapPostageTemplate.setPostageTemplateId(id);
                list.add(goodsMapPostageTemplate);
            }
            return goodsMapper.insertGoodsMapPostageTemplate(list);
        }
        return 0;
    }

    @Override
    public int deletePostageTemplate(String goodsId, String postageTemplateId) {
        GoodsMapPostageTemplate template=new GoodsMapPostageTemplate();
        template.setGoodsId(goodsId);
        template.setPostageTemplateId(postageTemplateId);
        return goodsDao.deletePostageTemplate(template);
    }

    @Override
    public List<PostageTemplate> queryPostageTemplateByGoodsId(String goodsId) {
        List<PostageTemplate> postageTemplates=postageTemplateDao.queryByGoodsId(goodsId);
        return postageTemplates;
    }

    @Override
    public int updateStatus(Goods goods) {
        return goodsDao.updateStatus(goods);
    }

    private void insertColors(Goods goods) {
        List<String> colors = goods.getGoodsColors();
        if (colors != null && colors.size() > 0) {
            List<GoodsColor> goodsColors = new ArrayList<>();
            for (String color : colors) {
                GoodsColor co = new GoodsColor(UUIDUtil.getUUID(), color, goods.getGoodsId());
                goodsColors.add(co);
            }
            goodsMapper.insertColors(goodsColors);
        }
    }

    private List<Activity> getActivity() {
        return activityService.queryValidActivity();
    }

}
